<?php

require_once(dirname(__FILE__) . '/appconf.php');

class DbMove extends Site {

    function setup() {
        $this->run_modes('do');
        $this->start_mode('do');
        $this->selfurl = '/dbMoveBlog.php';
        $this->pagesize = 200;

        set_time_limit(0);
        ob_end_clean();
        ob_implicit_flush(true);
        /*
          $this->blog_array = array(
          '1718' => 15,
          '20037' => 14,
          '20038' => 13,
          '20039' => 12,
          '20040' => 11,
          );
         * 
         */
        $this->blog_array = array(
            array('uid' => 47, 'blogid' => 5),
            array('uid' => 8020, 'blogid' => 57),
            array('uid' => 4913, 'blogid' => 45),
            array('uid' => 6025, 'blogid' => 47),
            array('uid' => 7302, 'blogid' => 110),
            array('uid' => 7617, 'blogid' => 262),
            array('uid' => 11335, 'blogid' => 263),
            array('uid' => 9458, 'blogid' => 266),
            array('uid' => 10454, 'blogid' => 267),
            array('uid' => 197, 'blogid' => 268),//10
            array('uid' => 11916, 'blogid' => 270),
            array('uid' => 7148, 'blogid' => 269),
            array('uid' => 8141, 'blogid' => 265),
            array('uid' => 7096, 'blogid' => 264),
            array('uid' => 1494, 'blogid' => 70),
            array('uid' => 1513, 'blogid' => 91),
            array('uid' => 7635, 'blogid' => 62),
            array('uid' => 1479, 'blogid' => 72),
            array('uid' => 7026, 'blogid' => 65),
            array('uid' => 985, 'blogid' => 93),//20
            array('uid' => 8359, 'blogid' => 94),
            array('uid' => 1924, 'blogid' => 96),
            array('uid' => 524, 'blogid' => 98),
            array('uid' => 212, 'blogid' => 99),
            array('uid' => 2207, 'blogid' => 101),
            array('uid' => 7347, 'blogid' => 74),
            array('uid' => 6022, 'blogid' => 76),
            array('uid' => 7536, 'blogid' => 103),
            array('uid' => 6941, 'blogid' => 77),
            array('uid' => 5810, 'blogid' => 105),//30
            array('uid' => 2831, 'blogid' => 54),
            array('uid' => 738, 'blogid' => 107),
            array('uid' => 1086, 'blogid' => 79),
            array('uid' => 65, 'blogid' => 80),
            array('uid' => 7087, 'blogid' => 69),
            array('uid' => 4879, 'blogid' => 81),
            array('uid' => 306, 'blogid' => 111),
            array('uid' => 7173, 'blogid' => 108),
            array('uid' => 3863, 'blogid' => 82),
            array('uid' => 58, 'blogid' => 84),
            array('uid' => 2724, 'blogid' => 85),
            array('uid' => 3838, 'blogid' => 113),
            array('uid' => 7705, 'blogid' => 87),
            array('uid' => 6276, 'blogid' => 114),
            array('uid' => 2128, 'blogid' => 116),
            array('uid' => 1781, 'blogid' => 38),
            array('uid' => 1090, 'blogid' => 117),
            array('uid' => 9065, 'blogid' => 162),
            array('uid' => 9066, 'blogid' => 163),
            array('uid' => 9068, 'blogid' => 165),
            array('uid' => 9082, 'blogid' => 180),
        );
        parent::setup();
    }

    function cgiapp_prerun() {

        require_once(LIB_PATH . '/Common/HTMLPurifier/HTMLPurifier.standalone.php');
        parent::cgiapp_prerun();
    }

    function action_do() {

        $page = $this->get_q('page', '1', 'int');
        $root = $this->param('WWW');

        $num = count($this->blog_array);
        if ($page > $num) {
            echo '完成全部轉換';
            exit();
        }
        $value = $this->blog_array[($page - 1)];


        echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
        echo $value['blogid'];
        $param = array();
        $param['field'] = "";
        $param['where'] = " post_status='publish' and post_type='post'";
        $param['order'] = " post_date asc";
        $param['group'] = "";
        $param['limit'] = "";
        $param['pagesize'] = "";
        $param['page'] = "";
        $param['join'] = "";
        $param['on'] = "";
        $data = db_query_hash('wp_' . $value['blogid'] . '_posts', $param, IMOREBLOGDB);


        if ($data['result']) {

            $config = HTMLPurifier_Config::createDefault();
            $config->set('HTML.TargetBlank', true);
            $config->set('HTML.Allowed', 'span[style],em,strong,img[src|align|height|width|alt|style],br,b');
            $HTMLPurifierObj = new HTMLPurifier($config);
//保留指定標籤，和標籤的屬性，其它全部過濾

            foreach ($data['result'] as $key_post => $value_post) {
                $value_post['ID'] . " , ";
                $content = $HTMLPurifierObj->purify($value_post['post_content']);

                $reg = "/<img[^>]*src=\"(http:\/\/(.+)\/(.+)\.(jpg|gif|bmp|bnp))\"/isU";
                preg_match_all($reg, $content, $img_array, PREG_PATTERN_ORDER);
                $img_array = array_unique($img_array[1]);

                if ($img_array) {
                    foreach ($img_array as $value_imgName) {
                        $ret = uploadfiles($root . '/upload/blog', $value_imgName);

                        if ($ret[0]['code'] == 1) {
                            $imgstr = str_replace($root, '', $ret[0]['path']);
                            $content = str_replace($value_imgName, $imgstr, $content);
                        }
                    }
                }


                $out = array();
                $pattern = '/\[gallery(.*?)\]/i';
                preg_match_all($pattern, $content, $out, PREG_PATTERN_ORDER);
                if (!empty($out[0][0])) {
                    $param = array();
                    $param['field'] = "";
                    $param['where'] = " post_status='inherit' and post_type='attachment' and post_parent=" . $value_post['ID'];
                    $param['order'] = " ID asc";
                    $param['group'] = "";
                    $param['limit'] = "";
                    $param['pagesize'] = "";
                    $param['page'] = "";
                    $param['join'] = "";
                    $param['on'] = "";
                    $data_img = db_query_hash('wp_' . $value['blogid'] . '_posts', $param, IMOREBLOGDB);
                    if ($data_img['result']) {
                        $str = "";

                        foreach ($data_img['result'] as $value_img) {

                            $ret = uploadfiles($root . '/upload/blog', $value_img['guid']);

                            if ($ret[0]['code'] == 1) {

                                $imgstr = str_replace($root, '', $ret[0]['path']);
                                $str .= " <img src=\"" . $imgstr . "\"> ";
                            }
                        }
                    }
                }

                $pattern_caption = '/\[caption(.*?)caption="(.*?)"(.*?)\](.*?)src="(.*?)"(.*?)caption\]/i';
                $replacement_caption = '<img src="${5}" /><br>${2}';
                $content = preg_replace($pattern_caption, $replacement_caption, $content);

                $pattern_s = '/\[gallery(.*?)\]/i';
                $replacement = '';
                $content = preg_replace($pattern_s, $replacement, $content);

                $insert_content = nl2br($content) . $str;

                $attr = array();
                $attr['mid'] = $value['uid'];
                $attr['title'] = $value_post['post_title'];
                $attr['content_json'] = "";
                $attr['body'] = $insert_content;
                $attr['is_valid'] = 1;
                $attr['is_accept'] = 1;
                $attr['status'] = 1;
                $attr['addtime'] = $value_post['post_date'];
                $attr['is_publish'] = 1;
                $attr['is_blog'] = 1;
                db_add('product_reports', $attr);
                echo $key_post . " 成功 " . $value_post['post_title'] . "<br>";
                flush();    //刷新输出缓冲 
            }

            echo '<meta http-equiv="refresh" content="1;url=dbMoveBlog.php?action=do&page=' . ($page + 1) . '">';
        } else {
            echo "無數據";
            echo '<meta http-equiv="refresh" content="1;url=dbMoveBlog.php?action=do&page=' . ($page + 1) . '">';
        }
    }

}

header("Content-type: text/html; charset=utf-8");
$webapp = new DbMove();
$webapp->run();
?>